package grow

import (
	"server/game/cache"
	"server/share/config"

	"github.com/hwcer/cosgo/registry"
	"github.com/hwcer/cosgo/slice"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
)

func init() {
	Register(&talent{})
}

// 芯片 装备
type talent struct {
}

func (this *talent) Caller(node *registry.Node, handle *context.Context) interface{} {
	f := node.Method().(func(*talent, *context.Context) interface{})
	return f(this, handle)
}

/**
 * @name setTalent
 * @param int rootNode 根节点ID[废弃]
 * @param int talentID 普通节点ID
 * 解锁天赋点
 */
func (this *talent) SetTalent(c *context.Context) any {
	id := c.GetInt32("talentID")
	playerTalent := config.Data.PlayerTalent[id]
	if playerTalent == nil {
		return errors.ErrConfigEmpty
	}
	roleDoc := cache.GetRole(c.Player.Updater)
	roleData := roleDoc.All()
	gid := playerTalent.Group

	if roleData.Talent[gid] == id {
		return nil
	}
	if beforeTalentNode := playerTalent.BeforeTalent; beforeTalentNode > 0 && roleData.Talent[gid] != beforeTalentNode {
		return c.Error("前置天赋未解锁")
	}

	var used int32
	data := make(map[int32]int32)
	for k, v := range roleData.Talent {
		if v > 0 {
			if ct := config.Data.PlayerTalent[v]; ct != nil {
				data[k] = v
				used += ct.TotPoint
			}
		}
	}

	playerLevel := config.Data.PlayerExp[roleData.Lv]
	if playerLevel == nil {
		return c.Errorf(0, "user level error:%v", roleData.Lv)
	}
	if n := playerLevel.Talent - used; n < playerTalent.SubPoint {
		return c.Error("talent point not enough")
	}
	data[gid] = id
	roleDoc.Set("talent", data)

	return nil
}

/**
* @name returnTalent
* 重置天赋点
 */
func (this *talent) ReturnTalent(c *context.Context) any {
	roleDoc := cache.GetRole(c.Player.Updater)
	roleDoc.Set("talent", map[int32]int32{})
	roleDoc.Set("useTalentBranch", []int32{})
	return nil
}

/**
 * @name setTalentTreeBranch
 * @param []int32 keys
 * 装备天赋树分支
 */
func (this *talent) SetTalentTreeBranch(c *context.Context) any {

	var keys []int32
	if err := c.Bind(&keys); err != nil {
		return err
	}
	keys = slice.Unrepeated(keys)

	if len(keys) > 2 {
		return c.Error("参数错误")
	}
	roleDoc := cache.GetRole(c.Player.Updater)
	roleData := roleDoc.All()
	for _, v := range keys {
		if roleData.Talent[v] == 0 {
			return c.Errorf(0, "该天赋分支根节点未解锁:%d", v)
		}
	}
	roleDoc.Set("useTalentBranch", keys)
	return nil
}

/**
 * @name setTeamTalentBranch
 * @param string teamId 编队唯一ID（类型+id）
 * @param string rootNodeStr 根结点ID 用','分割
 * 设置编队的天赋树分支
 */

func (this *talent) SetTeamTalentBranch(c *context.Context) any {
	id := c.GetInt32("teamId")
	if i := config.Data.Team[id]; i == nil {
		return errors.ErrConfigEmpty
	}

	s := c.GetString("rootNodeStr")
	arr := slice.SplitAndUnrepeated(s, ",")
	if len(arr) > 2 {
		return c.Error("参数错误")
	}
	roleDoc := cache.GetRole(c.Player.Updater)
	roleData := roleDoc.All()
	coll := c.Player.Collection(config.ITypeTeams)
	if len(arr) == 0 {
		coll.Set(id, "talent", []int32{})
		return nil //卸载
	}
	for _, v := range arr {
		if roleData.Talent[v] == 0 {
			return c.Errorf(0, "该天赋分支根节点未解锁:%d", v)
		}
	}

	coll.Set(id, "talent", arr)
	return nil
}
